unit tasacsproject;

interface

uses classes, tasacsabstract, contnrs, index, xtasacs, sysutils;

type
   TTasacsRun = class(TComponent, ITasacsRun)
   private
      m_RunName : string;
      m_ReadOnly : Boolean;
   protected
      function GetRunName : string;
      function GetProject : ITasacsProject;
   public
      constructor create(AOwner : TComponent; RunName : string; ReadOnly : Boolean); reintroduce;
   end; // TTasacsRun


   TWorkingRun = class(TTasacsRun)
   public
      constructor create(AOwner : TComponent; RunName : string); reintroduce;
   end;


   TArchiveRun = class(TTasacsRun)
   public
      constructor create(AOwner : TComponent; RunName : string); reintroduce;
   end;


   TTasacsObsData = class(TComponent, ITasacsObsData)
   private
      m_Category : Integer;
      m_Index : Integer;
      m_Name : string;
      m_Weight : TTasacsObsData;
   protected
      function GetCategory : Integer;
      function GetIndex : Integer;
      function GetName : string;
      function GetWeight : ITasacsObsData;
      function get_DataSourceValue(iYear, iAge : Integer) : double;
      procedure set_DataSourceValue(iYear, iAge : Integer; Value : double);
   public
      constructor create(AOwner : TComponent; Category, Index : Integer; Name : string); reintroduce;
   end; // TTasacsObsData


   TTasacsProject = class(TComponent, ITasacsProject)
   private
      m_ProjectName : string;
      m_WorkingRun : TTasacsRun;
      m_Runs : TObjectList;
      m_ObsData : TObjectList;
   protected
      function GetProjectName : string;
      function GetWorkingRun : ITasacsRun;
      function GetHistoryRun(I : Integer) : ITasacsRun;
      function GetNumHistoryRuns : Integer;
      function GetObsData(I : Integer) : ITasacsObsData;
      function GetNumObsData : Integer;
      procedure CreateObsData();
      procedure SaveRun();
      procedure RestoreRun(Run : ITasacsRun);
   public
      constructor create(AOwner : TComponent; ProjectName : string); reintroduce;
      destructor Destroy; override;
   end; // TTasacsProject


implementation


//////////////////////////
// TTasacsObsData


constructor TTasacsObsData.create(AOwner : TComponent; Category, Index : Integer; Name : string);
begin
   inherited Create(AOwner);
   m_Category := Category;
   m_Index := Index;
   m_Name := Name;
   if m_Category in [odCanum, odFleet, odSSB, odBiomass] then
      m_Weight := TTasacsObsData.create(Self, m_Category + 1, m_Index, m_Name + ' WEIGHT')
   else
      m_Weight := nil;
end; // TTasacsObsData.create



function TTasacsObsData.GetCategory : Integer;
begin
   Result := m_Category;
end; // TTasacsObsData.GetCategory


function TTasacsObsData.GetIndex : Integer;
begin
   Result := m_Index
end; // TTasacsObsData.GetIndex


function TTasacsObsData.GetName : string;
begin
   Result := m_Name;
end; // TTasacsObsData.GetName


function TTasacsObsData.GetWeight : ITasacsObsData;
begin
   Result := m_Weight;
end;


function TTasacsObsData.get_DataSourceValue(iYear, iAge : Integer) : double;
begin
   Result := xtasacs.get_DataSourceValue(m_Category, m_Index, iYear, iAge);
end; // TTasacsObsData.get_DataSourceValue


procedure TTasacsObsData.set_DataSourceValue(iYear, iAge : Integer; Value : double);
begin
   xtasacs.set_DataSourceValue(m_Category, m_Index, iYear, iAge, Value);
end; // TTasacsObsData.set_DataSourceValue


///////////////////////////////
// TTasacsRun


constructor TTasacsRun.create(AOwner : TComponent; RunName : string; ReadOnly : Boolean);
begin
   inherited Create(AOwner);
   m_ReadOnly := ReadOnly;
   m_RunName := RunName;
end; // TTasacsRun.create


function TTasacsRun.GetRunName : string;
begin
   Result := m_RunName;
end; // TTasacsRun.GetRunName


function TTasacsRun.GetProject : ITasacsProject;
begin
   Result := Owner as ITasacsProject;
end; // TTasacsRun.GetProject


////////////////////////////////
// TWorkingRun


constructor TWorkingRun.create(AOwner : TComponent; RunName : string);
begin
   inherited Create(AOwner, RunName, false);
end; // TWorkingRun.create


////////////////////////////////
// TArchiveRun


constructor TArchiveRun.create(AOwner : TComponent; RunName : string);
begin
   inherited Create(AOwner, RunName, true);
end; // TArchiveRun.create


//////////////////////////////////
// TTasacsProject


constructor TTasacsProject.create(AOwner : TComponent; ProjectName : string);
begin
   inherited Create(AOwner);
   m_ProjectName := ProjectName;
   m_WorkingRun := TWorkingRun.Create(Self, 'Working run');
   m_Runs := TObjectList.Create;
   m_ObsData := TObjectList.Create;

end; // TTasacsProject.create


procedure TTasacsProject.CreateObsData();
var
   I, Cat, Idx, NumIdx : Integer;
   name, namebase : string;
begin
   for I := 0 to length(c_DataSourceEditCategories) - 1 do
   begin
      Cat := c_DataSourceEditCategories[I];
      case (Cat) of
         odFleet:
            NumIdx := round(get_Run_Prop(propNumFleetIndices));
         odSSB:
            NumIdx := round(get_Run_Prop(propNumSSBIndices));
         odBiomass:
            NumIdx := round(get_Run_Prop(propNumBiomassIndices));
      else
         NumIdx := 1;
      end;
      namebase := c_DataSourcesetup[Cat].Key;
      for Idx := 0 to NumIdx - 1 do
      begin
         case (Cat) of
            odFleet, odSSB, odBiomass:
               name := namebase + ' ' + inttostr(Idx + 1);
         else
            name := namebase;
         end;
         m_ObsData.Add(TTasacsObsData.create(Self, Cat, Idx, Name));
      end;
   end;
end;


destructor TTasacsProject.Destroy;
begin
   m_WorkingRun.Free;
   m_Runs.Free;
   m_ObsData.Free;
   inherited;
end; // TTasacsProject.Destroy


function TTasacsProject.GetProjectName : string;
begin
   Result := m_ProjectName;
end; // TTasacsProject.GetProjectName


function TTasacsProject.GetWorkingRun : ITasacsRun;
begin
   Result := m_WorkingRun;
end; // TTasacsProject.GetWorkingRun


function TTasacsProject.GetHistoryRun(I : Integer) : ITasacsRun;
begin
   Result := TComponent(m_Runs[I]) as ITasacsRun;
end; // TTasacsProject.GetHistoryRun


function TTasacsProject.GetNumHistoryRuns : Integer;
begin
   Result := m_Runs.count;
end;  // TTasacsProject.GetNumHistoryRuns


function TTasacsProject.GetObsData(I : Integer) : ITasacsObsData;
begin
   Result := TComponent(m_ObsData[I]) as ITasacsObsData;
end;


function TTasacsProject.GetNumObsData : Integer;
begin
   Result := m_ObsData.Count;
end; // TTasacsProject.GetNumObsData


procedure TTasacsProject.SaveRun();
(* Save the current run to history
*)
begin
end; // TTasacsProject.SaveRun


procedure TTasacsProject.RestoreRun(Run : ITasacsRun);
(* Restore the current run from history
*)
begin
end; // TTasacsProject.RestoreRun


end.
